mui.init({
	beforeback: function() {
		var list = plus.webview.currentWebview("../bookdetails/details.html").opener();
		mui.fire(list, 'refresh');
		return true;
	},
	pullRefresh: {
		container: '#offCanvasContentScroll',
		down: {
			callback: pulldownRefresh
		},
		up: {
			contentrefresh: '正在加载...',
			callback: pullupRefresh
		}
	}
});
var bookshelf = new Vue({
	el: '.main',
	data: {
		booksl: [],
		pingfen: [],
		trues: []
	},
	updated: function() {
		var kaiguan = false
		$(".add").on("tap", function(e) {
			if(kaiguan) {
				return
			}
			var self = $(this)
			kaiguan = true
			common.ajax("student/myShelf/addBook", {
				uid: common.uid,
				token: common.token,
				bookUid: $(this).parents(".mainclass").attr("bookuid")
			}, function(data) {
				if(data.code == 200) {
					mui.toast("添加成功")
					kaiguan = false
					self.addClass("added")
					self.text("已添加")
					var load = plus.webview.getWebviewById("bookshelf.html");
					mui.fire(load, "index");
					var go = plus.webview.getWebviewById("../stacks/Recommended.html")
					mui.fire(go, "refresh")
				} else {
					mui.toast(data.message)
					kaiguan = false
				}
			})
			e.stopPropagation()
		})
		$(".mainclass").on("tap", function() {
			common.open("../bookdetails/details.html", "../bookdetails/details.html", $(this).attr("bookuid"))
		})
	}
});
var temp = 1
/**
 * 下拉刷新具体业务实现
 */
function pulldownRefresh() {
	setTimeout(function() {
		mui('#offCanvasContentScroll').pullRefresh().endPulldownToRefresh()
	}, 400)

}
/**
 * 上拉加载具体业务实现
 */
var seach = localStorage.getItem("seach")

function pullupRefresh() {
	var self = this
	var grad = $(".selectiontype").eq(0).text() == "适合年级" ? "" : $(".selectiontype").eq(0).text()
	var types = $(".selectiontype").eq(1).attr("uid") == null ? "" : $(".selectiontype").eq(1).attr("uid")
	common.ajax('student/bookCity/queryAllBook', {
		uid: common.uid,
		token: common.token,
		keyWord: seach,
		page: temp++,
		pageSize: "8"
	}, function(data) {
		if(data.code == 200) {
			if(!data.data.hasNextPage) {
				mui('#offCanvasContentScroll').pullRefresh().endPullupToRefresh(true)
			} else {
				mui('#offCanvasContentScroll').pullRefresh().endPullupToRefresh()
			}
			datafor(data)
		} else {
			mui.toast(data.message)
		}
	}, 'get')
}
if(mui.os.plus) {
	mui.plusReady(function() {
		setTimeout(function() {
			mui('#offCanvasContentScroll').pullRefresh().pullupLoading();
		}, 1000);

	});
} else {
	mui.ready(function() {
		mui('#offCanvasContentScroll').pullRefresh().pullupLoading();
	});
}

function datafor(data) {
	for(var i = 0; i < data.data.list.length; i++) {
		if(String(data.data.list[i].mark).indexOf(".") != -1) {
			bookshelf.trues.push("scoring_two")
		} else {
			bookshelf.trues.push("")
		}
		bookshelf.booksl.push(data.data.list[i])
		bookshelf.pingfen.push(Math.floor(data.data.list[i].mark))
	}
}
$(".seach_val").on("focus", function() {
	mui.back()
	$(this).blur();
})